package com.liuyucheng.util;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.z.ZCompressorInputStream;

import java.io.*;

/**
 * 自动解压.Z格式的文件
 */
public class ZCompress {

    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) {
        ZCompress zip = new ZCompress();
        zip.uncompress("C:\\Users\\LiuYucheng\\Desktop\\cate.dat.Z");
    }


    /**
     * 获取文件夹下全部文件
     *
     * @param file
     */
    public static void findAllfile(String file) {
        ZCompress zip = new ZCompress();
        File dir = new File(file);
        File[] subs = dir.listFiles();
        for (File sub : subs) {
            String newfile = sub.getParent() + File.separator + sub.getName();
            zip.uncompress(newfile);
        }
    }

    /**
     * .Z文件解压
     *
     * @param file
     */
    public void uncompress(String file) {
        ZCompress.deCompressTZFile(file);
    }

    /**
     * 将String型转换为File类型
     *
     * @param file
     * @return
     */
    public static File deCompressTZFile(String file) {
        return deCompressZFile(new File(file));
    }

    /**
     * 执行解压
     *
     * @param file
     * @return
     */
    private static File deCompressZFile(File file) {
        String absolutePath = file.getAbsolutePath();
        System.out.println("absolutePath = " + absolutePath);
        int buffersize = 2048;
        FileOutputStream out = null;
        ZCompressorInputStream zIn = null;
        try {
            FileInputStream fin = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fin);
            //解压后的文件存放路径及文件名
//            String name = file.getName().substring(0, file.getName().indexOf("."));
            String name = absolutePath.replace(".Z","");
            System.out.println("name = " + name);
            File outFile = new File(name);
            out = new FileOutputStream(outFile);
            zIn = new ZCompressorInputStream(in);
            final byte[] buffer = new byte[buffersize];
            int n = 0;
            while (-1 != (n = zIn.read(buffer))) {
                out.write(buffer, 0, n);
            }
            return outFile;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                out.close();
                zIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * .TAR格式文件解压
     *
     * @param file
     */
    private static void deCompressTARFile(File file) {
        int buffersize = 2048;
        String basePath = file.getParent() + File.separator;
        TarArchiveInputStream is = null;
        try {
            is = new TarArchiveInputStream(new FileInputStream(file));
            while (true) {
                TarArchiveEntry entry = is.getNextTarEntry();
                if (entry == null) {
                    break;
                }
                if (entry.isDirectory()) {// 这里貌似不会运行到，跟ZipEntry有点不一样
                    new File(basePath + entry.getName()).mkdirs();
                } else {
                    FileOutputStream os = null;
                    try {
                        File f = new File(basePath + entry.getName());
                        if (!f.getParentFile().exists()) {
                            f.getParentFile().mkdirs();
                        }
                        if (!f.exists()) {
                            f.createNewFile();
                        }
                        os = new FileOutputStream(f);
                        byte[] bs = new byte[buffersize];
                        int len = -1;
                        while ((len = is.read(bs)) != -1) {
                            os.write(bs, 0, len);
                        }
                        os.flush();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        os.close();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                file.delete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}